//
//  HDHTabBarController.m
//  无忧社区
//
//  Created by hdh1997 on 15/11/7.
//  Copyright © 2015年 hdh1997. All rights reserved.
//

#import "HDHTabBarController.h"
#import "HDHHomeController.h"
#import "HDHNeighbouringController.h"
#import "HDHPrivatismController.h"
#import "HDHSettingController.h"
#import "HDHshoppingCartController.h"

#import "HDHTabBar.h"
#import "HDHNavigationController.h"


@interface HDHTabBarController()<HDHTabBarDelegate>

@property(nonatomic,strong) NSMutableArray *items;

@end

@implementation HDHTabBarController

-(NSMutableArray *)items
{

    if (_items == nil) {
        _items = [NSMutableArray array];
    }
    return _items;
}


//初始化控制器

-(void)viewDidLoad
{
    [super viewDidLoad];
    
    
    //给tabBarVc添加所有子控制器
    [self setUpAllChidViewController];
    
    //自定义tabBar
    [self setUptabBar];

}

#pragma mark ---------------------------
#pragma mark -自定义tabBar

-(void)setUptabBar
{

    //移除系统的tabBar
    [self.tabBar removeFromSuperview];
    
    
    //添加自己的tabBar
    HDHTabBar *tabBar = [[HDHTabBar alloc]init];
    
    //设置代理
    tabBar.delegate = self;
    
    
    //给tabBar传递UITabBarItem模型数组
    tabBar.items = self.items;
    
    tabBar.backgroundColor = [UIColor whiteColor];
    tabBar.frame = self.tabBar.frame;
    [self.view addSubview:tabBar];

}




#pragma mark ---------------------------
#pragma mark -HDHTabBarDelegate

-(void)tabBar:(HDHTabBar *)tabBar didClickBtn:(NSInteger)index
{
    
    //切换控制器
    self.selectedIndex = index;

}




#pragma mark ---------------------------
#pragma mark -给tabBarVc添加所有子控制器

-(void)setUpAllChidViewController
{
    
    //主页
    HDHHomeController *home = [[HDHHomeController alloc]init];
    [self setUpOneChildViewController:home image:[UIImage imageNamed:@"home_nol"] selImage:[UIImage imageNamed:@"home_select"] title:@"主页"];
    
    //个人中心
    HDHPrivatismController *privatism = [[HDHPrivatismController alloc]init];
    [self setUpOneChildViewController:privatism image:[UIImage imageNamed:@"my_nol"] selImage:[UIImage imageNamed:@"my_select"] title:@"个人中心"];
    
    //周边
    HDHNeighbouringController *neighbouring = [[HDHNeighbouringController alloc]init];
    [self setUpOneChildViewController:neighbouring image:[UIImage imageNamed:@"neighbouring_nol"] selImage:[UIImage imageNamed:@"neighbouring_select"] title:@"周边"];
    
    //设置
    HDHSettingController *setting = [[HDHSettingController alloc]init];
    [self setUpOneChildViewController:setting image:[UIImage imageNamed:@"deploy_nol"] selImage:[UIImage imageNamed:@"deploy_select"] title:@"设置"];
    
    
    //购物车
    HDHshoppingCartController *shopping = [[HDHshoppingCartController alloc]init];
    [self setUpOneChildViewController:shopping image:[UIImage imageNamed:@"gouwu"] selImage:[UIImage imageNamed:@"gouwu"] title:@"购物车"];
    
    


}

#pragma mark - 添加一个的子控制器(封装这个方法使代码更简洁)

-(void)setUpOneChildViewController:(UIViewController *)vc image:(UIImage *)image selImage:(UIImage *)selImage title:(NSString *)title
{
    //这段代码是用来添加一个子控制器的
    UINavigationController *nav = [[HDHNavigationController alloc] initWithRootViewController:vc];
    
    
//    //竞技场,应该使用它自己的导航控制器
//    if ([vc isKindOfClass:[HDHArenaController class]]) {
//        nav = [[HDHArenaNavigationController alloc]initWithRootViewController:vc];
//    }
    [nav.navigationBar setBackgroundImage:[UIImage imageNamed:@"title_bg"] forBarMetrics:UIBarMetricsDefault];
    
    //把对应按钮的图片保存到对应子控制器的TabBarItem
    nav.tabBarItem.image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    nav.tabBarItem.selectedImage = [selImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    //设置导航条的标题
    vc.title = title;
    
    [self.items addObject:nav.tabBarItem];
    
    [self addChildViewController:nav];
    
}







@end
